package org.mxp.service;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> list() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select id, loginname, username, password, status,  DATE_FORMAT(birthday, '%Y-%m-%d') birthday, sex, update_time, create_time, email from t_user");
        return list;
    }

    public int add(Map<String, Object> info) {
        int insert = jdbcTemplate.update("insert into t_user (id, loginname, username, password, status, birthday, sex, create_time, email) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
                UUID.randomUUID().toString(), MapUtils.getString(info, "loginname"), MapUtils.getString(info, "username"), MapUtils.getString(info, "password"),
                MapUtils.getString(info, "status"), MapUtils.getString(info, "birthday"), MapUtils.getString(info, "sex"), new Date(), MapUtils.getString(info, "email"));
        return insert;
    }
}
